**Figure 2a

clear
set more off
set scheme s2mono

set seed 2020
********************************************************************************
* Cleaning & Reshaping endline data from HH to student level
********************************************************************************

use "$merge_scores", clear
drop control treatment1 treatment2 mshwariorlsa

count 
assert r(N)==3787 //merged children

merge m:1 pseudo_idvar using "$merge_mobile", keepusing(_merge_baseline_endline treatment_arm stratification b_schoolname_baseline_encoded) gen(merge_whisker) update



** Create indicator variable for treatment
gen control=0
replace control=1 if treatment_arm==0
replace control=0 if treatment_arm!=0
replace control=. if treatment_arm==.
label variable control "Control"

gen treatment1=0
replace treatment1=1 if treatment_arm==1
replace treatment1=. if treatment_arm==.
label variable treatment1 "MBA Treatment"

gen treatment2=0
replace treatment2=1 if treatment_arm==2
replace treatment2=. if treatment_arm==.
label variable treatment2 "LSA Treatment"

gen mshwariorlsa=0 
replace mshwariorlsa=1 if (treatment_arm==1 | treatment_arm==2)
replace mshwariorlsa=. if treatment_arm==. 
lab variable mshwariorlsa "MBA treatment"




**Using kp_binary function to create enrollment_imputatioin variables
local prefix = 1
forvalues i = -0.5(0.05)0.51 {

	display `i'
	display `prefix'
	kl_binary, ind_var("enroll") treatment("mshwariorlsa") gen("enroll_") prefix(`prefix') sd(`i')
	
local prefix = `prefix' + 1	
}

  
preserve
	clear
	tempfile results
	save `results', replace emptyok
restore 

 
forvalues i = 1/21{
	preserve
		local varlab: variable label enroll_`i'
		reg enroll_`i' mshwariorlsa i.stratification, vce(cluster b_schoolname_baseline_encoded)
		parmest , norestore
		keep parm estimate min95 max95 p
		keep if parm =="mshwariorlsa"
		gen label = "`varlab'"
		gen var = "enroll_`i'"

		append using `results'
		save `results', replace

	restore
	}

use `results', clear
  
gen rank = _n

sort label

split label, parse(" ") gen(v)
drop v1 v2 v3 v5 v6
destring v4, replace
gen bs = abs(v4)
sort bs v4
bysort bs (v4): gen j = _n 
egen grp = group(bs)

keep estimate v4 bs j grp

reshape wide estimate v4, i(grp) j(j)

replace estimate2 = estimate1 if estimate2==.

sort bs
gen rank = _n
  
twoway (rcap  estimate2 estimate1 rank, title(Enrollment) ytitle("OLS Coefficient") ylabel(0(0.02)0.12, angle(0)) yline(0) sort xlabel(1 "0 sd" 3  "0.1 sd" 5 "0.2 sd" 7 "0.3 sd" 9 "0.4 sd" 11 "0.5 sd")) 


graph export "$wd/03_output/graph_02a_enrollment.png", as(png) name("Graph")
exit 

 


